package org.utils.ui;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.MissingResourceException;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public class ResourceUtil
{

	private ResourceUtil()
	{
	}

	public static Frame findFrame(Component component)
	{
		while (component != null && !(component instanceof Frame))
			component = component.getParent();
		return (Frame) component;
	}

	public static Window findWindow(Component component)
	{
		while (component != null && !(component instanceof Window))
			component = component.getParent();
		return (Window) component;
	}

	public static Icon loadIcon(String s)
	{
		return loadIcon(s, true);
	}

	public static Icon loadIcon(String s, boolean flag)
	{
		ImageIcon imageicon = null;
		if (s != null)
		{
			URL url = loadResource(s);
			if (url != null)
				imageicon = new ImageIcon(url);
		}
		if (imageicon == null && flag)
			throw new MissingResourceException("The icon '" + s + "' was not found.\n"
			        + "   Partial paths must be relative to the classpath or applet codebase.\n"
			        + "   Full paths must be either an absolute URL or a full file path.", "ResourceUtil", s);
		return imageicon;
	}

	public static Image loadImage(String s)
	{
		return loadImage(s, null);
	}

	public static Image loadImage(String s, Component component)
	{
		URL url = loadResource(s);
		Image image = null;
		if (url != null)
		{
			Toolkit toolkit;
			if (component != null)
				toolkit = component.getToolkit();
			else
				toolkit = Toolkit.getDefaultToolkit();
			image = toolkit.getImage(url);
		}
		if (url != null && image != null)
		{
			String s1 = url.getProtocol();
			if (s1 != null && (s1.equals("file") || s1.equals("systemresource")))
				waitForImage(image, component, s);
		}
		else
		{
			throw new MissingResourceException("The image '" + s + "' was not found.\n"
			        + "   Partial paths must be relative to the classpath or applet codebase.\n"
			        + "   Full paths must be either an absolute URL or a full file path.", "ResourceUtil", s);
		}
		return image;
	}

	public static URL loadResource(String s)
	{
		URL url = pathToURL(s);
		if (url == null)
			url = ClassLoader.getSystemResource(s);
		return url;
	}

	private static URL pathToURL(String s)
	{
		try
		{
			return new URL(s);
		}
		catch (MalformedURLException _ex)
		{
			return null;
		}
	}

	public static void waitForImage(Image image)
	{
		waitForImage(image, null);
	}

	public static void waitForImage(Image image, Component component)
	{
		waitForImage(image, null, null);
	}

	public static void waitForImage(Image image, Component component, String s)
	{
		ImageTracker imagetracker = new ImageTracker(image, component);
		imagetracker.waitForImage();
		if (imagetracker.gotError())
			Logger.getLogger(ResourceUtil.class).warn(
			        "Could not load image '" + (s == null ? image.toString() : s) + "'.");
	}
}
